<template>
    <div>
        <div id="free-class">
            <div class="class-list-container">
                <!-- 课程标题行 -->
                <div class="class-title-line">
                    <div class="class-title">
                        <img v-if="type == 'free'" src="@/assets/images/free-logo.png" alt="">
                        <img v-else-if="type == 'boutique'" src="@/assets/images/fire.png" alt="">
                        <img v-else-if="type == 'discount'" src="@/assets/images/clock.png" alt="">

                        <span v-if="type == 'free'">免费课程</span>
                        <span v-else-if="type == 'boutique'">精品课程</span>
                        <span v-else-if="type == 'discount'">优惠课程</span>
                    </div>
                    <div class="more-btn">
                        <span @click="goToSearch(type)">
                            更多
                            <img src="@/assets/images/箭头.svg" alt="">
                        </span>
                    </div>
                </div>

                <!-- 课程列表 -->
                <div class=" class-list-container class-list-box">

                    <div class=" class-list-item" v-for="item in list" :key="item.id"
                        @click="goToCourseDetail(item.id)">

                        <!-- 课程图片 -->
                        <div class="img-container">
                            <img :src="item.coverImage" alt="">
                        </div>

                        <div class="class-title"> {{ item.courseName }}</div>

                        <div class="class-info"> 共 {{ item.chapterCount }} 节课| {{ item.viewCount }} 人报名 </div>

                        <div class="class-price">
                            <div>
                                <span class="free" v-if="item.isFree == 1">免费</span>
                                <span class="price" v-if="item.isFree!=1">
                                    ￥{{ item.isDiscount == 0 ? item.price : item.discountPrice }}
                                </span>
                                <span class="old-price" v-if="item.isFree != 1 && item.isDiscount == 1">
                                    ￥{{ item.price }}
                                </span>
                            </div>
                            <span v-if="item.isFree != 1 && item.isDiscount == 1" class="discount-info">限时钜惠</span>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</template>

<script>
import { getList } from '@/api/request'
export default {
    name: 'CourseList',
    data() {
        return {
            list: []
        }
    },
    props: {
        type: {
            type: String,
            required: true,
        },
        pageSize: {
            type: Number,
            default: 1
        },
        pageNum: {
            type: Number,
            default: 1
        }
    },
    created() {
        const params={
            type: this.type,
            pageSize:this.pageSize,
            pageNum:this.pageNum
        }
        getList(params).then((result) => {
            this.list = result.data.rows;
        }).catch((err) => {
            console.log(err);
        });

    },
    methods: {
        goToCourseDetail(courseId) {
            this.$router.push({
                name: 'detail',
                params: {
                    id: courseId
                }
            })
        },
        goToSearch() {
            this.$router.push({
                path: '/search',
                query: {
                    pageNum: 1,
                    pageSize: 15,
                    type: this.type,
                    subjectId: '',
                    keyword: '',
                    courseType: ''
                }
            })

        }
    }
}
</script>

<style lang="less" scoped>
.class-list-container {
    width: 1160px;
    background-color: white;
    margin: 0 auto;
    margin-top: 20px;
    border-radius: 20px;
    padding: 10px;


    .class-title-line {
        height: 50px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 2px solid #e8e8e8;
        padding: 0 12px 10px 0;
        margin-top: 10px;
        margin-left: 10px;

        .class-title {
            display: flex;
            align-items: center;

            >img:nth-child(1) {
                width: 40px;
                height: 40px;
                margin-right: 15px;
            }

            >span {
                font-size: 24px;
                line-height: 1.5;
            }

            >img:nth-child(3) {

                cursor: pointer;
                margin-left: 8px;
                width: 93px;
                height: 33.5px;
            }
        }

        .more-btn {
            display: flex;
            align-items: center;

            >a {
                color: #999;
                font-size: 16px;
                line-height: 34px;
            }

            >img {
                width: 16px;
            }
        }
    }

    .class-list-container {
        display: flex;
        width: 1140px;
        justify-content: space-between;
    }




    .class-list-item {
        margin-bottom: 10px;
        box-shadow: 0 4px 10px 0 rgba(0, 0, 0, .15);
        border-radius: 10px;
        color: #000;
        padding-bottom: 10px;

        >.img-container {
            margin-bottom: 10px;
            overflow: hidden;
            border-radius: 10px 10px 0 0;

            >img {
                width: 215px;
                height: 118px;
                transition: all 0.3s;

                &:hover {
                    transform: scale(1.1);
                }
            }
        }

        >.class-title {
            padding-left: 10px;
            margin-bottom: 10px;
            font-size: 14px;
            width: 215px;
            /* 文字在一行显示，多余隐藏，用省略号表示 */
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;

            &:hover {
                color: green;
            }
        }

        >.class-info {
            color: #b7bbc4;
            height: 22px;
            box-sizing: border-box;
            line-height: 1.5;
            padding-left: 10px;
            margin-bottom: 10px;
        }

        >.class-price {
            padding: 0 10px;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .free {
                color: #00cf8c;
                font-weight: 600;
                font-size: 16px;
            }

            .price {
                color: #ff4500;
                font-weight: 500;
                line-height: 1.5;
            }

            .old-price {
                color: #999;
                text-decoration: line-through;
                font-size: 12px;
            }

            .discount-info {
                color: #fa8c16;
                background: #fff7e6;
                border-color: #ffd591;
                border: 1px solid #d9d9d9;
                padding: 0 7px;
                font-size: 12px;
                box-sizing: border-box;
            }
        }

    }

}

a {
    text-decoration: none;
}
</style>
